/*** THIS PROGRAM AIMS AT:
- redefining all variations into three outcomes: decrease, stability or increase;
- identifying price trajectories and price spells;
- computing cumulative sums and lagged values of input price changes, production changes, etc. over price spells ;
- computing lagged values of variables to be used as instruments in the estimation;
- defining observed increases and decreases as permanent or transitory; 

The input from Laurent Baudry, Thomas Heckel and Sylvie Tarrieu is gratefully aknowledged. 
****/ 

DM 'CLEAR LOG; CLEAR OUTPUT;';

libname temp 'C:\Users\SAMIC\Documents\Version_FINALE\Envoi_Final_REStat\Envoi_pgm\Verif_data';

options linesize = 100;
options pagesize = 72;


proc sort data=TEMP.ACEMO_EC; by siren etabl ape annee mois; run;

/***************************************************************************/
/* REDEFINITION OF VARIATIONS INTO EITHER DECREASE, NO CHANGE OR INCREASE  */
/***************************************************************************/

data TEMP.ACEMO_EC_NEW; set TEMP.ACEMO_EC;
	array VARQUAL(8) EVCOM EVPRMP EVPRPF PREVPRO EVPRO EVSTPF EVLIV PREVSTPF ;
	do I = 1 to 8; 
		if      VARQUAL(I) IN (1.0, 2.0, 2.5) then VARQUAL(I) = +1;   
		else if VARQUAL(I) = 3.0 then VARQUAL(I) = +0;   
		else if VARQUAL(I) IN (3.5, 4.0, 5.0) then VARQUAL(I) = -1;   
		else if VARQUAL(I) = 0.0 then VARQUAL(I) =  .;   
		end; 
	if isalmtot2 ne . then isalmtot2 = (isalmtot2 > 0) - (isalmtot2 <0);
    run; 

data TEMP.ACEMO_EC_NEW; set TEMP.ACEMO_EC_NEW;
if EVPRPF ne . ; 
ident =siren||etabl||ape;
run;

proc sort data=TEMP.ACEMO_EC_NEW; by ident annee mois; run;


/*******************************************/
/*  IDENTIFICATION OF PRICE TRAJECTORIES   */
/*******************************************/

data TEMP.ACEMO_EC_NEW; set TEMP.ACEMO_EC_NEW;
numenq=0;
if annee= 1996 & mois = 1 then numenq=1;
if annee= 1996 & mois = 2 then numenq=2;
if annee= 1996 & mois = 3 then numenq=3;
if annee= 1996 & mois = 4 then numenq=4;
if annee= 1996 & mois = 5 then numenq=5;
if annee= 1996 & mois = 6 then numenq=6;
if annee= 1996 & mois = 9 then numenq=7;
if annee= 1996 & mois = 10 then numenq=8;
if annee= 1996 & mois = 11 then numenq=9;
if annee= 1996 & mois = 12 then numenq=10;


if annee= 1997 & mois = 1 then numenq=11;
if annee= 1997 & mois = 2 then numenq=12;
if annee= 1997 & mois = 3 then numenq=13;
if annee= 1997 & mois = 4 then numenq=14;
if annee= 1997 & mois = 5 then numenq=15;
if annee= 1997 & mois = 6 then numenq=16;
if annee= 1997 & mois = 9 then numenq=17;
if annee= 1997 & mois = 10 then numenq=18;
if annee= 1997 & mois = 11 then numenq=19;
if annee= 1997 & mois = 12 then numenq=20;

if annee= 1998 & mois = 1 then numenq=21;
if annee= 1998 & mois = 2 then numenq=22;
if annee= 1998 & mois = 3 then numenq=23;
if annee= 1998 & mois = 4 then numenq=24;
if annee= 1998 & mois = 5 then numenq=25;
if annee= 1998 & mois = 6 then numenq=26;
if annee= 1998 & mois = 7 then numenq=27;
if annee= 1998 & mois = 9 then numenq=28;
if annee= 1998 & mois = 10 then numenq=29;
if annee= 1998 & mois = 11 then numenq=30;
if annee= 1998 & mois = 12 then numenq=31;

if annee= 1999 & mois = 1 then numenq=32;
if annee= 1999 & mois = 2 then numenq=33;
if annee= 1999 & mois = 3 then numenq=34;
if annee= 1999 & mois = 4 then numenq=35;
if annee= 1999 & mois = 5 then numenq=36;
if annee= 1999 & mois = 6 then numenq=37;
if annee= 1999 & mois = 7 then numenq=38;
if annee= 1999 & mois = 9 then numenq=39;
if annee= 1999 & mois = 10 then numenq=40;
if annee= 1999 & mois = 11 then numenq=41;
if annee= 1999 & mois = 12 then numenq=42;

if annee= 2000 & mois = 1 then numenq=43;
if annee= 2000 & mois = 2 then numenq=44;
if annee= 2000 & mois = 3 then numenq=45;
if annee= 2000 & mois = 4 then numenq=46;
if annee= 2000 & mois = 5 then numenq=47;
if annee= 2000 & mois = 6 then numenq=48;
if annee= 2000 & mois = 7 then numenq=49;
if annee= 2000 & mois = 9 then numenq=50;
if annee= 2000 & mois = 10 then numenq=51;
if annee= 2000 & mois = 11 then numenq=52;
if annee= 2000 & mois = 12 then numenq=53;

if annee= 2001 & mois = 1 then numenq=54;
if annee= 2001 & mois = 2 then numenq=55;
if annee= 2001 & mois = 3 then numenq=56;
if annee= 2001 & mois = 4 then numenq=57;
if annee= 2001 & mois = 5 then numenq=58;
if annee= 2001 & mois = 6 then numenq=59;
if annee= 2001 & mois = 7 then numenq=60;
if annee= 2001 & mois = 9 then numenq=61;
if annee= 2001 & mois = 10 then numenq=62;
if annee= 2001 & mois = 11 then numenq=63;
if annee= 2001 & mois = 12 then numenq=64;

if annee= 2002 & mois = 1 then numenq=65;
if annee= 2002 & mois = 2 then numenq=66;
if annee= 2002 & mois = 3 then numenq=67;
if annee= 2002 & mois = 4 then numenq=68;
if annee= 2002 & mois = 5 then numenq=69;
if annee= 2002 & mois = 6 then numenq=70;
if annee= 2002 & mois = 7 then numenq=71;
if annee= 2002 & mois = 9 then numenq=72;
if annee= 2002 & mois = 10 then numenq=73;
if annee= 2002 & mois = 11 then numenq=74;
if annee= 2002 & mois = 12 then numenq=75;

if annee= 2003 & mois = 1 then numenq=76;
if annee= 2003 & mois = 2 then numenq=77;
if annee= 2003 & mois = 3 then numenq=78;
if annee= 2003 & mois = 4 then numenq=79;
if annee= 2003 & mois = 5 then numenq=80;
if annee= 2003 & mois = 6 then numenq=81;
if annee= 2003 & mois = 7 then numenq=82;
if annee= 2003 & mois = 9 then numenq=83;
if annee= 2003 & mois = 10 then numenq=84;
if annee= 2003 & mois = 11 then numenq=85;
if annee= 2003 & mois = 12 then numenq=86;

if annee= 2004 & mois = 1 then numenq=87;
if annee= 2004 & mois = 2 then numenq=88;
if annee= 2004 & mois = 3 then numenq=89;
if annee= 2004 & mois = 4 then numenq=90;
if annee= 2004 & mois = 5 then numenq=91;
if annee= 2004 & mois = 6 then numenq=92;
if annee= 2004 & mois = 7 then numenq=93;
if annee= 2004 & mois = 9 then numenq=94;
if annee= 2004 & mois = 10 then numenq=95;
if annee= 2004 & mois = 11 then numenq=96;
if annee= 2004 & mois = 12 then numenq=97;

if annee= 2005 & mois = 1 then numenq=98;
if annee= 2005 & mois = 2 then numenq=99;
if annee= 2005 & mois = 3 then numenq=100;
if annee= 2005 & mois = 4 then numenq=101;
if annee= 2005 & mois = 5 then numenq=102;
if annee= 2005 & mois = 6 then numenq=103;
if annee= 2005 & mois = 7 then numenq=104;
if annee= 2005 & mois = 9 then numenq=105;
if annee= 2005 & mois = 10 then numenq=106;
if annee= 2005 & mois = 11 then numenq=107;
if annee= 2005 & mois = 12 then numenq=108;
run;

* ANY GROUP OF STRICTLY SUCCESSIVE OBSERVATIONS FOR A GIVEN FIRM/PLANT/INDUSTRY(PRODUCT) IS A PRICE TRAJECTORY;

data TEMP.ACEMO_EC2; set TEMP.ACEMO_EC_NEW; by SIREN ETABL APE ANNEE MOIS; 
	LANNEE = lag(ANNEE); LMOIS = lag(MOIS);
	if first.APE then do; 
		DEB_ANNEE = ANNEE; DEB_MOIS = MOIS; 
		FIN_ANNEE = ANNEE; FIN_MOIS = MOIS; 
		end; 
	if not(first.APE) 
	   and ((ANNEE=LANNEE and MOIS=LMOIS+1) or 
		    (ANNEE=LANNEE+1 and MOIS=1 and LMOIS=12) or 
			(ANNEE=LANNEE   and MOIS=9 and LMOIS=6 and ANNEE< 1998) or 
			(ANNEE=LANNEE   and MOIS=9 and LMOIS=7 and ANNEE>=1998)) then do; 
		FIN_ANNEE = ANNEE; FIN_MOIS = MOIS; 
		end; 
	if not(first.APE) 
	   and not((ANNEE=LANNEE and MOIS=LMOIS+1) or 
		       (ANNEE=LANNEE+1 and MOIS=1 and LMOIS=12) or 
			   (ANNEE=LANNEE   and MOIS=9 and LMOIS=6 and ANNEE< 1998) or 
			   (ANNEE=LANNEE   and MOIS=9 and LMOIS=7 and ANNEE>=1998)) then do; 
		DEB_ANNEE = ANNEE; DEB_MOIS = MOIS; 
		FIN_ANNEE = ANNEE; FIN_MOIS = MOIS; 
		end; 
	retain DEB_ANNEE DEB_MOIS;
	run; 

data TEMP.DEB_FIN; set TEMP.ACEMO_EC2(keep = SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS); by SIREN ETABL APE DEB_ANNEE DEB_MOIS; 
	if last.DEB_MOIS; 
	run;

data TEMP.DEB_FIN2; set TEMP.DEB_FIN(where=((FIN_ANNEE-DEB_ANNEE)*12+FIN_MOIS-DEB_MOIS+1>=12)); 
	ANNEE = DEB_ANNEE; MOIS = DEB_MOIS; 
	do while (ANNEE<FIN_ANNEE or (ANNEE=FIN_ANNEE and MOIS<=FIN_MOIS)); 
		output; 
		if MOIS=6 and ANNEE<1998 then MOIS=9; 
		else if MOIS=7 and ANNEE>=1998 then MOIS=9; 
		else if MOIS=12 then do; MOIS=1; ANNEE=ANNEE+1; end; 
		else MOIS=MOIS+1; 
		end; 
	run;

data TEMP.ACEMO_EC3; merge TEMP.ACEMO_EC_NEW TEMP.DEB_FIN2(in=AGARDER); 
	by SIREN ETABL APE ANNEE MOIS; if AGARDER; 
	run;  

proc sort data=TEMP.ACEMO_EC3; by  ident  annee mois; run;

data TEMP.ACEMO_EC3; set TEMP.ACEMO_EC3; 
duree_traj=(fin_annee-deb_annee)*12+ Fin_mois-deb_mois+1;
duree_t = (annee-deb_annee)*12+mois-deb_mois+1;
debut_traj=deb_annee||deb_mois;
fin_traj=fin_annee|fin_mois;
ident_traj=SIREN||ETABL||APE||debut_traj;
run;

data TEMP.nb_traj; set TEMP.ACEMO_EC3; by ident_traj;
if first.ident_traj;
run;

Proc sort data= TEMP.ACEMO_EC3; by ident_traj;run;

data TEMP.first_obs(keep=duree_traj ident_traj y_null); set TEMP.ACEMO_EC3; by ident_traj;
if first.ident_traj;
y_null=evprpf;
run;

data TEMP.ACEMO_EC3BIS ; merge TEMP.first_obs TEMP.ACEMO_EC3; by ident_traj;
run;


/*******************************************/
/*  IDENTIFICATION OF PRICE SPELLS         */
/*******************************************/

proc sort data = TEMP.ACEMO_EC3BIS; 
	by SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS ANNEE MOIS; 
	run;  


data TEMP.ACEMO_EC4; set TEMP.ACEMO_EC3BIS; 
	by SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS ANNEE MOIS; 
	LEVPRPF = lag(evprpf);
 
	if first.DEB_MOIS then do; 
		DEB2_ANNEE = ANNEE; DEB2_MOIS = MOIS; 
		FIN2_ANNEE = ANNEE; FIN2_MOIS = MOIS; 
		end; 
	if not(first.DEB_MOIS) and LEVPRPF = 0 then do; 
		FIN2_ANNEE = ANNEE; FIN2_MOIS = MOIS; 
		end; 
	if not(first.DEB_MOIS) and LEVPRPF ne 0 then do; 
		DEB2_ANNEE = ANNEE; DEB2_MOIS = MOIS; 
		FIN2_ANNEE = ANNEE; FIN2_MOIS = MOIS; 
		end; 
	retain DEB2_ANNEE DEB2_MOIS; 
	run;

data TEMP.DEB2_FIN2; set TEMP.ACEMO_EC4(keep = SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS DEB2_ANNEE DEB2_MOIS FIN2_ANNEE FIN2_MOIS); 
	by SIREN ETABL APE DEB_ANNEE DEB_MOIS DEB2_ANNEE DEB2_MOIS; 
	if last.DEB2_MOIS; 
	run;

data TEMP.DEB2_FIN2_2; set TEMP.DEB2_FIN2; 
	ANNEE = DEB2_ANNEE; MOIS = DEB2_MOIS; 
	do while (ANNEE<FIN2_ANNEE or (ANNEE=FIN2_ANNEE and MOIS<=FIN2_MOIS)); 
		output; 
		if MOIS=6  and ANNEE<1998  then MOIS=9; 
		else if MOIS=7  and ANNEE>=1998 then MOIS=9; 
		else if MOIS=12 then do; MOIS=1; ANNEE=ANNEE+1; end; 
		else MOIS=MOIS+1; 
		end; 
	run;



data TEMP.ACEMO_EC5; merge TEMP.ACEMO_EC3BIS TEMP.DEB2_FIN2_2; 
	by SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS ANNEE MOIS;
	DUREE = (ANNEE-DEB2_ANNEE)*12+MOIS-DEB2_MOIS+1;
	run;  

data temp.acemo_ec5; set temp.acemo_ec5;
duree_spell=(fin2_annee-deb2_annee)*12+ Fin2_mois-deb2_mois+1;
debut_spell=deb2_annee||deb2_mois;
fin_spell=fin2_annee|fin2_mois;
ident_spell=SIREN||ETABL||APE||debut_spell;
run;

data TEMP.verif_spell; set TEMP.ACEMO_EC5; by ident_spell;
if first.ident_spell;
run;

data TEMP.ACEMO_EC6; set TEMP.ACEMO_EC5; 
	if ISALMTOT2 ne . and MOIS not in (1,4,7,10) then ISALMTOT2 = 0; 
	run; 

/******************************************************************/
/*  COMPUTATION OF CUMULATIVE SUMS OF CHANGES OVER PRICE SPELLS   */
/******************************************************************/

proc sort data = TEMP.ACEMO_EC6; by SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS DEB2_ANNEE DEB2_MOIS FIN2_ANNEE FIN2_MOIS ANNEE MOIS; run;  

data TEMP.ACEMO_EC7(drop=LIPP2 ); set TEMP.ACEMO_EC6; 
	by SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS DEB2_ANNEE DEB2_MOIS FIN2_ANNEE FIN2_MOIS ANNEE MOIS; 

LIPP2 = lag(IPP2); 
if first.DEB_MOIS then do; LIPP2=.; end; 
if LIPP2 not in (0,.) then TXIPP2=100*log(IPP2/LIPP2);  
if txipp2 ne . then prind_var=(-1)*(txipp2 <0)+(0)*(txipp2 =0)+(1)*(txipp2 >0);
else prind_var = . ;
txipp2 = prind_var;

    if first.DEB2_MOIS then do; 
		CUM_ISALMTOT = ISALMTOT2; 
		CUM_EVPRMP = EVPRMP; 
		CUM_EVCOM = EVCOM; 
		CUM_IPP2 = TXIPP2;
		CUM_EVPRO = EVPRO; 
		CUM_EVSTPF = EVSTPF; 
		CUM_EVLIV = EVLIV; 
  		end; 
   if not(first.DEB2_MOIS) then do; 
		CUM_ISALMTOT = CUM_ISALMTOT+ISALMTOT2; 
		CUM_EVPRMP = CUM_EVPRMP+EVPRMP;
		CUM_EVCOM = CUM_EVCOM+EVCOM;
		CUM_IPP2 = CUM_IPP2+TXIPP2; 
		CUM_EVPRO = CUM_EVPRO+EVPRO; 
 		CUM_EVSTPF = CUM_EVSTPF+EVSTPF; 
		CUM_EVLIV = CUM_EVLIV+EVLIV; 
		end; 
	retain CUM_ISALMTOT CUM_EVPRMP CUM_EVCOM CUM_IPP2 CUM_EVPRO CUM_EVSTPF CUM_EVLIV ;  
	
run;


/*********************************************************************************************************/
/*  COMPUTATION OF LEAD VALUES, LAGGED VALUES AND PARTIAL CUMULATIVE SUMS OF CHANGES OVER PRICE SPELLS   */
/*********************************************************************************************************/

proc sort data = TEMP.ACEMO_EC7; 
by SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS DEB2_ANNEE DEB2_MOIS FIN2_ANNEE FIN2_MOIS ANNEE MOIS; run;  
data TEMP.LX; set TEMP.ACEMO_EC7
(keep=	SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS DEB2_ANNEE DEB2_MOIS FIN2_ANNEE FIN2_MOIS ANNEE MOIS
		ISALMTOT2 EVPRMP EVCOM TXIPP2 EVPRO EVSTPF EVLIV );
	run;  
		
data TEMP.L1; set TEMP.LX; 
		if MOIS=6 and ANNEE<1998 then MOIS=9; 
		else if MOIS=7 and ANNEE>=1998 then MOIS=9; 
		else if MOIS=12 then do; MOIS=1; ANNEE=ANNEE+1; end; 
		else MOIS=MOIS+1; 
	run;  
data TEMP.L2; set TEMP.L1; 
		if MOIS=6 and ANNEE<1998 then MOIS=9; 
		else if MOIS=7 and ANNEE>=1998 then MOIS=9; 
		else if MOIS=12 then do; MOIS=1; ANNEE=ANNEE+1; end; 
		else MOIS=MOIS+1; 
	run;  
data TEMP.L3; set TEMP.L2; 
		if MOIS=6 and ANNEE<1998 then MOIS=9; 
		else if MOIS=7 and ANNEE>=1998 then MOIS=9; 
		else if MOIS=12 then do; MOIS=1; ANNEE=ANNEE+1; end; 
		else MOIS=MOIS+1; 
	run;  
data TEMP.L4; set TEMP.L3; 
		if MOIS=6 and ANNEE<1998 then MOIS=9; 
		else if MOIS=7 and ANNEE>=1998 then MOIS=9; 
		else if MOIS=12 then do; MOIS=1; ANNEE=ANNEE+1; end; 
		else MOIS=MOIS+1; 
	run;  
data TEMP.L5; set TEMP.L4; 
		if MOIS=6 and ANNEE<1998 then MOIS=9; 
		else if MOIS=7 and ANNEE>=1998 then MOIS=9; 
		else if MOIS=12 then do; MOIS=1; ANNEE=ANNEE+1; end; 
		else MOIS=MOIS+1; 
	run;  
data TEMP.L6; set TEMP.L5; 
		if MOIS=6 and ANNEE<1998 then MOIS=9; 
		else if MOIS=7 and ANNEE>=1998 then MOIS=9; 
		else if MOIS=12 then do; MOIS=1; ANNEE=ANNEE+1; end; 
		else MOIS=MOIS+1; 
	run;  

data TEMP.ACEMO_EC8; merge TEMP.ACEMO_EC7(in=DS_ACEMO_EC) 
TEMP.L1(rename=(ISALMTOT2=ISALMTOT2_L1 EVPRMP=EVPRMP_L1 EVCOM=EVCOM_L1 TXIPP2=TXIPP2_L1 EVPRO=EVPRO_L1 EVSTPF=EVSTPF_L1 EVLIV=EVLIV_L1  ))
TEMP.L2(rename=(ISALMTOT2=ISALMTOT2_L2 EVPRMP=EVPRMP_L2 EVCOM=EVCOM_L2 TXIPP2=TXIPP2_L2 EVPRO=EVPRO_L2 EVSTPF=EVSTPF_L2 EVLIV=EVLIV_L2  ))
TEMP.L3(rename=(ISALMTOT2=ISALMTOT2_L3 EVPRMP=EVPRMP_L3 EVCOM=EVCOM_L3 TXIPP2=TXIPP2_L3 EVPRO=EVPRO_L3 EVSTPF=EVSTPF_L3 EVLIV=EVLIV_L3  ))
TEMP.L4(rename=(ISALMTOT2=ISALMTOT2_L4 EVPRMP=EVPRMP_L4 EVCOM=EVCOM_L4 TXIPP2=TXIPP2_L4 EVPRO=EVPRO_L4 EVSTPF=EVSTPF_L4 EVLIV=EVLIV_L4  ))
TEMP.L5(rename=(ISALMTOT2=ISALMTOT2_L5 EVPRMP=EVPRMP_L5 EVCOM=EVCOM_L5 TXIPP2=TXIPP2_L5 EVPRO=EVPRO_L5 EVSTPF=EVSTPF_L5 EVLIV=EVLIV_L5  ))
TEMP.L6(rename=(ISALMTOT2=ISALMTOT2_L6 EVPRMP=EVPRMP_L6 EVCOM=EVCOM_L6 TXIPP2=TXIPP2_L6 EVPRO=EVPRO_L6 EVSTPF=EVSTPF_L6 EVLIV=EVLIV_L6  )); 
by SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS DEB2_ANNEE DEB2_MOIS FIN2_ANNEE FIN2_MOIS ANNEE MOIS; 
if DS_ACEMO_EC; 
run;  

data TEMP.FX; set TEMP.LX
(keep= SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS ANNEE MOIS ISALMTOT2 EVPRMP EVCOM TXIPP2 EVPRO EVSTPF EVLIV);
run;  

data TEMP.F1; set TEMP.FX; 
		if MOIS=9 and ANNEE<1998 then MOIS=6; 
		else if MOIS=9 and ANNEE>=1998 then MOIS=7; 
		else if MOIS=1 then do; MOIS=12; ANNEE=ANNEE-1; end; 
		else MOIS=MOIS-1; 
	run;  

data TEMP.ACEMO_EC9; merge TEMP.ACEMO_EC8(in=DS_ACEMO_EC) 
TEMP.F1(rename=(ISALMTOT2=ISALMTOT2_F1 EVPRMP=EVPRMP_F1 EVCOM=EVCOM_F1 TXIPP2=TXIPP2_F1 EVPRO=EVPRO_F1 EVSTPF=EVSTPF_F1 EVLIV=EVLIV_F1)); 
by SIREN ETABL APE DEB_ANNEE DEB_MOIS FIN_ANNEE FIN_MOIS ANNEE MOIS; 
if DS_ACEMO_EC; 
run;  

data TEMP.ACEMO_EC10; set TEMP.ACEMO_EC9; 
	array VAR1(6) ISALMTOT2_L1-ISALMTOT2_L6; 	do I=1 to 6; if VAR1(I)=. then VAR1(I)=0; end; 
	array VAR5(6) EVPRMP_L1-EVPRMP_L6; 			do I=1 to 6; if VAR5(I)=. then VAR5(I)=0; end; 
	array VAR6(6) EVCOM_L1-EVCOM_L6;			do I=1 to 6; if VAR6(I)=. then VAR6(I)=0; end; 
	array VAR7(6) TXIPP2_L1-TXIPP2_L6; 			do I=1 to 6; if VAR7(I)=. then VAR7(I)=0; end; 
    array VAR9(6) EVPRO_L1-EVPRO_L6; 			do I=1 to 6; if VAR9(I)=. then VAR9(I)=0; end; 
    array VAR10(6) EVSTPF_L1-EVSTPF_L6; 		do I=1 to 6; if VAR10(I)=. then VAR10(I)=0; end; 
    array VAR11(6) EVLIV_L1-EVLIV_L6; 			do I=1 to 6; if VAR11(I)=. then VAR11(I)=0; end; 

    CUM_ISALMTOT_L123 = CUM_ISALMTOT-ISALMTOT2-ISALMTOT2_L1-ISALMTOT2_L2-ISALMTOT2_L3; 
    CUM_EVPRMP_L123 = CUM_EVPRMP-EVPRMP-EVPRMP_L1-EVPRMP_L2-EVPRMP_L3; 
	CUM_EVPRO_L123 = CUM_EVPRO-EVPRO-EVPRO_L1-EVPRO_L2-EVPRO_L3; 
	CUM_EVCOM_L123 = CUM_EVCOM-EVCOM-EVCOM_L1-EVCOM_L2-EVCOM_L3; 
	CUM_IPP2_L123 = CUM_IPP2-TXIPP2-TXIPP2_L1-TXIPP2_L2-TXIPP2_L3; 
	CUM_EVLIV_L123 = CUM_EVLIV-EVLIV-EVLIV_L1-EVLIV_L2-EVLIV_L3; 
	CUM_EVSTPF_L123 = CUM_EVSTPF-EVSTPF-EVSTPF_L1-EVSTPF_L2-EVSTPF_L3; 
	run;

/******************************************************************/
/*  COMPUTATION OF LAGGED VALUES TO BE USED AS INSTRUMENTS        */
/******************************************************************/

proc sort data = TEMP.ACEMO_EC10; by SIREN ETABL APE ANNEE MOIS; run; 

data TEMP.ACEMO_EC10; set TEMP.ACEMO_EC10;
l1_ident =lag( ident );  l1_numenq=lag(numenq);
l2_ident =lag2( ident ); l2_numenq=lag2(numenq);
l3_ident =lag3( ident ); l3_numenq=lag3(numenq);
l4_ident =lag4( ident ); l4_numenq=lag4(numenq);
l5_ident =lag5( ident ); l5_numenq=lag5(numenq);
l6_ident =lag6( ident ); l6_numenq=lag6(numenq);
l7_ident =lag7( ident ); l7_numenq=lag7(numenq);
l8_ident =lag8( ident ); l8_numenq=lag8(numenq);
run;

proc sort data = TEMP.ACEMO_EC10; by SIREN ETABL APE descending ANNEE descending MOIS; run; 

data TEMP.ACEMO_EC10; set TEMP.ACEMO_EC10;
f1_ident =lag( ident );
f1_numenq=lag(numenq);

proc sort data = TEMP.ACEMO_EC10; by SIREN ETABL APE ANNEE MOIS; run; 

data TEMP.ACEMO_EC10; set TEMP.ACEMO_EC10;
lead1_ok = 0;
lag1_ok = 0;
lag2_ok = 0;
lag3_ok = 0;
lag4_ok = 0;
lag5_ok = 0;
lag6_ok = 0;
lag7_ok = 0;
lag8_ok = 0;

IF ( ident =f1_ident  & numenq=f1_numenq-1) then lead1_ok=1;
IF ( ident =l1_ident  & numenq=l1_numenq+1) then lag1_ok=1;
IF ( ident =l2_ident  & numenq=l2_numenq+2) then lag2_ok=1;
IF ( ident =l3_ident  & numenq=l3_numenq+3) then lag3_ok=1;
IF ( ident =l4_ident  & numenq=l4_numenq+4) then lag4_ok=1;
IF ( ident =l5_ident  & numenq=l5_numenq+5) then lag5_ok=1;
IF ( ident =l6_ident  & numenq=l6_numenq+6) then lag6_ok=1;
IF ( ident =l7_ident  & numenq=l7_numenq+7) then lag7_ok=1;
IF ( ident =l8_ident  & numenq=l8_numenq+8) then lag8_ok=1;

data TEMP.ACEMO_EC10; set TEMP.ACEMO_EC10;
L1_EVPRPF=lag(evprpf);
L1_evcom=lag(evcom);
L1_evliv=lag(evliv);
L1_evpro=lag(evpro);
L1_evprmp=lag(evprmp);
L1_ISALMTOT2=lag(isalmtot2);
L1_EVSTPF = lag(evstpf);
L1_txipp2=lag(txipp2);

L2_EVPRPF=lag2(evprpf);
L2_evcom=lag2(evcom);
L2_evliv=lag2(evliv);
L2_evpro=lag2(evpro);
L2_evprmp=lag2(evprmp);
L2_ISALMTOT2=lag2(isalmtot2);
L2_EVSTPF = lag2(evstpf);
L2_txipp2=lag2(txipp2);

L3_EVPRPF=lag3(evprpf);
L3_evcom=lag3(evcom);
L3_evliv=lag3(evliv);
L3_evpro=lag3(evpro);
L3_evprmp=lag3(evprmp);
L3_ISALMTOT2=lag3(isalmtot2);
L3_EVSTPF = lag3(evstpf);
L3_txipp2=lag3(txipp2);

L4_EVPRPF=lag4(evprpf);
L4_evcom=lag4(evcom);
L4_evliv=lag4(evliv);
L4_evpro=lag4(evpro);
L4_evprmp=lag4(evprmp);
L4_ISALMTOT2=lag4(isalmtot2);
L4_EVSTPF = lag4(evstpf);
L4_txipp2=lag4(txipp2);

L5_EVPRPF=lag5(evprpf);
L5_evcom=lag5(evcom);
L5_evliv=lag5(evliv);
L5_evpro=lag5(evpro);
L5_evprmp=lag5(evprmp);
L5_ISALMTOT2=lag5(isalmtot2);
L5_EVSTPF = lag5(evstpf);
L5_txipp2=lag5(txipp2);

L6_EVPRPF=lag6(evprpf);
L6_evcom=lag6(evcom);
L6_evliv=lag6(evliv);
L6_evpro=lag6(evpro);
L6_evprmp=lag6(evprmp);
L6_ISALMTOT2=lag6(isalmtot2);
L6_EVSTPF = lag6(evstpf);
L6_txipp2=lag6(txipp2);

L7_EVPRPF=lag7(evprpf);
L7_evcom=lag7(evcom);
L7_evliv=lag7(evliv);
L7_evpro=lag7(evpro);
L7_evprmp=lag7(evprmp);
L7_ISALMTOT2=lag7(isalmtot2);
L7_EVSTPF = lag7(evstpf);
L7_txipp2=lag7(txipp2);

L8_EVPRPF=lag8(evprpf);
L8_evcom=lag8(evcom);
L8_evliv=lag8(evliv);
L8_evpro=lag8(evpro);
L8_evprmp=lag8(evprmp);
L8_ISALMTOT2=lag8(isalmtot2);
L8_EVSTPF = lag8(evstpf);
run;

 
data temp.acemo_EC10; set temp.acemo_EC10; by ident_spell;
first_obs_spell = first.ident_spell;
ident = siren||etabl||ape;
censure1=(deb2_annee=annee)*(deb2_mois=mois)*(l1_evprpf=0 or l1_evprpf = .);
run;

data temp.liste_spells; set temp.acemo_EC10; by ident;  
if first_obs_spell = 1;
first_spell = first.ident;
run;

data temp.liste_spells(keep=ident_spell left_censor); set temp.liste_spells; by ident;  
censure2=(first_spell = 1);
censure = censure1+censure2;
if censure ne 0  then left_censor=1;
run;

data temp.acemo_EC10; merge temp.acemo_EC10 temp.liste_spells; by ident_spell;
run;

data temp.acemo_EC10; set temp.acemo_EC10;
if left_censor ne 1 then left_censor=0;


/***********************************/
/*  CREATION OF SPECIFIC DUMMIES   */
/***********************************/

data TEMP.ACEMO_EC10; set TEMP.ACEMO_EC10;

DUR_1  = (DUREE=1); 
DUR_2  = (DUREE=2); 
DUR_3  = (DUREE=3); 
DUR_4  = (DUREE=4); 
DUR_5  = (DUREE=5); 
DUR_6  = (DUREE=6); 
DUR_7  = (DUREE=7); 
DUR_8  = (DUREE=8); 
DUR_9  = (DUREE=9); 
DUR_10 = (DUREE=10); 
DUR_11 = (DUREE=11); 
DUR_12 = (DUREE=12); 
DUR_13 = (DUREE=13); 
DUR_14 = (DUREE=14); 
DUR15_24 = (DUREE>=15 and DUREE<=24); 
DUR25_00 = (DUREE>24); 

MOIS_1  = (MOIS=1); 
MOIS_2  = (MOIS=2); 
MOIS_3  = (MOIS=3); 
MOIS_4  = (MOIS=4); 
MOIS_5  = (MOIS=5); 
MOIS_6  = (MOIS=6); 
MOIS_7  = (MOIS=7); 
MOIS_8  = (MOIS=8); 
MOIS_9  = (MOIS=9); 
MOIS_10 = (MOIS=10); 
MOIS_11 = (MOIS=11); 
MOIS_12 = (MOIS=12); 

ANNEE_1998  = (ANNEE=1998); 
ANNEE_1999  = (ANNEE=1999); 
ANNEE_2000  = (ANNEE=2000); 
ANNEE_2001  = (ANNEE=2001); 
ANNEE_2002  = (ANNEE=2002); 
ANNEE_2003  = (ANNEE=2003); 
ANNEE_2004  = (ANNEE=2004); 
ANNEE_2005  = (ANNEE=2005); 

trim1=mois_1+mois_2+mois_3;
trim2=mois_4+mois_5+mois_6;
trim3=mois_7+mois_8+mois_9;
trim4=mois_10+mois_11+mois_12;

tva_2000=(mois=4 & annee=2000);
tva_2000_2=(mois<=4 & annee=2000);

euro_2002=(mois=1 & annee=2002);
euro_2002_2=(mois>=7 & annee=2001)+(mois<=6 & annee=2002);

run;

proc sort data=temp.acemo_EC10; by naf_mc; run;

data temp.acemo_EC10; merge temp.acemo_EC10(in=in1) temp.table_passage_nomenclatures; by naf_mc; 
if in1;
run;

data temp.acemo_EC11 ; set temp.acemo_EC10;
ind_DA = (naf_gc = 'DA');
ind_DB = (naf_gc = 'DB');
ind_DC = (naf_gc = 'DC');
ind_DD = (naf_gc = 'DD');
ind_DE = (naf_gc = 'DE');
ind_DG = (naf_gc = 'DG');
ind_DH = (naf_gc = 'DH');
ind_DI = (naf_gc = 'DI');
ind_DJ = (naf_gc = 'DJ');
ind_DK = (naf_gc = 'DK');
ind_DL = (naf_gc = 'DL');
ind_DM = (naf_gc = 'DM');
ind_DN = (naf_gc = 'DN');
run;

/*************************************************************************************************/
/*  ELIMINATION OF ALL OBSERVATIONS CORRESPONDING TO LEFT-CENSORED SPELLS, OF SHORT TRAJECTORIES,*/ 
/*  OR FOR WHICH LEADS OR LAGS ARE MISSING OR NOT PROPERLY DEFINED                               */
/*************************************************************************************************/

data temp.complet ; set temp.acemo_EC11;
if lead1_ok = 1  & lag1_ok=1 & lag2_ok=1 & lag3_ok=1 & lag4_ok=1 & lag5_ok=1 & lag6_ok=1  & lag7_ok=1 & lag8_ok=1 ;
if evprpf ne . & evprmp ne . & evpro ne . & evcom ne . & evstpf ne . & txipp2 ne . & isalmtot2 ne . &
l1_evprmp ne . & l2_evprmp ne . & l3_evprmp ne . & l4_evprmp ne . & l5_evprmp ne . & l6_evprmp ne . & l7_evprmp ne .  & l8_evprmp ne .  &
l1_evstpf ne . & l2_evstpf ne . & l3_evstpf ne . & l4_evstpf ne . & l5_evstpf ne . & l6_evstpf ne . & l7_evstpf ne .  & l8_evstpf ne .  &
l1_evpro ne . & l2_evpro ne . & l3_evpro ne . & l4_evpro ne . & l5_evpro ne . & l6_evpro ne . & l7_evpro ne .  & l8_evpro ne .  &
l1_evcom ne . & l2_evcom ne . & l3_evcom ne . & l4_evcom ne . & l5_evcom ne . & l6_evcom ne . & l7_evcom ne .  & l8_evcom ne .  &
l1_txipp2 ne . & l2_txipp2 ne . & l3_txipp2 ne . & l4_txipp2 ne . & l5_txipp2 ne . & l6_txipp2 ne . & 
l1_isalmtot2 ne . & l2_isalmtot2 ne . & l3_isalmtot2 ne . & l4_isalmtot2 ne . & l5_isalmtot2 ne . & l6_isalmtot2 ne . & l7_isalmtot2 ne .  & l8_isalmtot2 ne .  & 
prevpro ne . & evprmp_f1 ne . & evcom_f1 ne . & prevstpf ne . & isalmtot2_f1 ne . & txipp2_f1 ne . ; 
IF CUM_ISALMTOT NE .;
IF CUM_EVPRMP NE .;
IF CUM_EVCOM NE .; 
IF CUM_IPP2 NE .;
IF CUM_EVPRO NE .; 
IF CUM_EVSTPF NE .; 
IF NAF_GC in (' ', 'DF') then delete;
if duree_traj >= 12;
if left_censor=0;
run;

proc sort data=temp.COMPLET; by ident_spell annee mois;
run;

data temp.stata_base_q3 ; set temp.complet;
ident_new=substr(ident, 1,31);
run;

data temp.stata_base_q3 ; set temp.stata_base_q3;
format ident_stata2 F30.0;
ident_stata2=input(compress(siren||etabl||debut_traj||fin_traj),F30.0);
run;

proc sort data=temp.stata_base_q3; by ident_stata2 numenq;
run;

data temp.stata_base_q3; set temp.stata_base_q3; 
if ident_stata2=lag(ident_stata2) & numenq=lag(numenq) then delete;
ident_stata=ident_stata2;
run;


/*******************************************************************/
/*  DEFINITION OF PERMANENT OR TRANSITORY INCREASES OR DECREASES   */ 
/*******************************************************************/

data temp.stata_base_q3_asym; set temp.stata_base_q3;

evprpf_up = (evprpf >0 ) * evprpf;

evprmp_f1_up = (evprmp_f1 >0 ) * evprmp_f1;
evprmp_up = (evprmp >0 ) * evprmp;
evprmp_l1_up = (evprmp_l1 >0 ) * evprmp_l1;
evprmp_l2_up = (evprmp_l2 >0 ) * evprmp_l2;
evprmp_l3_up = (evprmp_l3 >0 ) * evprmp_l3;
cum_evprmp_l123_up = (cum_evprmp_l123 >0 ) * cum_evprmp_l123;

isalmtot2_f1_up = (isalmtot2_f1 >0 ) * isalmtot2_f1;
isalmtot2_up = (isalmtot2 >0 ) * isalmtot2;
isalmtot2_l1_up = (isalmtot2_l1 >0 ) * isalmtot2_l1;
isalmtot2_l2_up = (isalmtot2_l2 >0 ) * isalmtot2_l2;
isalmtot2_l3_up = (isalmtot2_l3 >0 ) * isalmtot2_l3;
cum_isalmtot_l123_up = (cum_isalmtot_l123 >0 ) * cum_isalmtot_l123;

prevpro_up = (prevpro >0 ) * prevpro;
evpro_up = (evpro >0 ) * evpro;
evpro_l1_up = (evpro_l1 >0 ) * evpro_l1;
evpro_l2_up = (evpro_l2 >0 ) * evpro_l2;
evpro_l3_up = (evpro_l3 >0 ) * evpro_l3;
cum_evpro_l123_up = (cum_evpro_l123 >0 ) * cum_evpro_l123;

evcom_f1_up = (evcom_f1 >0 ) * evcom_f1;
evcom_up = (evcom >0 ) * evcom;
evcom_l1_up = (evcom_l1 >0 ) * evcom_l1;
evcom_l2_up = (evcom_l2 >0 ) * evcom_l2;
evcom_l3_up = (evcom_l3 >0 ) * evcom_l3;
cum_evcom_l123_up = (cum_evcom_l123 >0 ) * cum_evcom_l123;

txipp2_f1_up = (txipp2_f1 >0 ) * txipp2_f1;
txipp2_up = (txipp2 >0 ) * txipp2;
txipp2_l1_up = (txipp2_l1 >0 ) * txipp2_l1;
txipp2_l2_up = (txipp2_l2 >0 ) * txipp2_l2;
txipp2_l3_up = (txipp2_l3 >0 ) * txipp2_l3;
cum_ipp2_l123_up = (cum_ipp2_l123 >0 ) * cum_ipp2_l123;
      
evstpf_f1_up = (evstpf_f1 >0 ) * evstpf_f1;
evstpf_up = (evstpf >0 ) * evstpf;
evstpf_l1_up = (evstpf_l1 >0 ) * evstpf_l1;
evstpf_l2_up = (evstpf_l2 >0 ) * evstpf_l2;
evstpf_l3_up = (evstpf_l3 >0 ) * evstpf_l3;
cum_evstpf_l123_up = (cum_evstpf_l123 >0 ) * cum_evstpf_l123;

evprpf_down = (evprpf <0 ) * evprpf;

evprmp_f1_down = (evprmp_f1 <0 ) * evprmp_f1;
evprmp_down = (evprmp <0 ) * evprmp;
evprmp_l1_down = (evprmp_l1 <0 ) * evprmp_l1;
evprmp_l2_down = (evprmp_l2 <0 ) * evprmp_l2;
evprmp_l3_down = (evprmp_l3 <0 ) * evprmp_l3;
cum_evprmp_l123_down = (cum_evprmp_l123 <0 ) * cum_evprmp_l123;

isalmtot2_f1_down = (isalmtot2_f1 <0 ) * isalmtot2_f1;
isalmtot2_down = (isalmtot2 <0 ) * isalmtot2;
isalmtot2_l1_down = (isalmtot2_l1 <0 ) * isalmtot2_l1;
isalmtot2_l2_down = (isalmtot2_l2 <0 ) * isalmtot2_l2;
isalmtot2_l3_down = (isalmtot2_l3 <0 ) * isalmtot2_l3;
cum_isalmtot_l123_down = (cum_isalmtot_l123 <0 ) * cum_isalmtot_l123;

prevpro_down = (prevpro <0 ) * prevpro;
evpro_down = (evpro <0 ) * evpro;
evpro_l1_down = (evpro_l1 <0 ) * evpro_l1;
evpro_l2_down = (evpro_l2 <0 ) * evpro_l2;
evpro_l3_down = (evpro_l3 <0 ) * evpro_l3;
cum_evpro_l123_down = (cum_evpro_l123 <0 ) * cum_evpro_l123;

evcom_f1_down = (evcom_f1 <0 ) * evcom_f1;
evcom_down = (evcom <0 ) * evcom;
evcom_l1_down = (evcom_l1 <0 ) * evcom_l1;
evcom_l2_down = (evcom_l2 <0 ) * evcom_l2;
evcom_l3_down = (evcom_l3 <0 ) * evcom_l3;
cum_evcom_l123_down = (cum_evcom_l123 <0 ) * cum_evcom_l123;

txipp2_f1_down = (txipp2_f1 <0 ) * txipp2_f1;
txipp2_down = (txipp2 <0 ) * txipp2;
txipp2_l1_down = (txipp2_l1 <0 ) * txipp2_l1;
txipp2_l2_down = (txipp2_l2 <0 ) * txipp2_l2;
txipp2_l3_down = (txipp2_l3 <0 ) * txipp2_l3;
cum_ipp2_l123_down = (cum_ipp2_l123 <0 ) * cum_ipp2_l123;
      
evstpf_f1_down = (evstpf_f1 <0 ) * evstpf_f1;
evstpf_down = (evstpf <0 ) * evstpf;
evstpf_l1_down = (evstpf_l1 <0 ) * evstpf_l1;
evstpf_l2_down = (evstpf_l2 <0 ) * evstpf_l2;
evstpf_l3_down = (evstpf_l3 <0 ) * evstpf_l3;
cum_evstpf_l123_down = (cum_evstpf_l123 <0 ) * cum_evstpf_l123;

L1_evpro_up= (l1_evpro >0)* l1_evpro;
L1_evcom_up= (l1_evcom >0)* l1_evcom;
L1_evprmp_up= (l1_evprmp >0) * l1_evprmp;
L1_ISALMTOT2_up= (l1_isalmtot2 >0)* l1_isalmtot2;
L1_TXIPP2_up = (l1_txipp2 >0) * l1_txipp2;
L1_evstpf_up= (l1_evstpf >0)* l1_evstpf;

L2_evpro_up= (l2_evpro >0)* l2_evpro;
L2_evcom_up= (l2_evcom >0)* l2_evcom;
L2_evprmp_up= (l2_evprmp >0) * l2_evprmp;
L2_ISALMTOT2_up= (l2_isalmtot2 >0)* l2_isalmtot2;
L2_TXIPP2_up = (l2_txipp2 >0) * l2_txipp2;
L2_evstpf_up= (l2_evstpf >0)* l2_evstpf;

L3_evpro_up= (l3_evpro >0)* l3_evpro;
L3_evcom_up= (l3_evcom >0)* l3_evcom;
L3_evprmp_up= (l3_evprmp >0) * l3_evprmp;
L3_ISALMTOT2_up= (l3_isalmtot2 >0)* l3_isalmtot2;
L3_TXIPP2_up = (l3_txipp2 >0) * l3_txipp2;
L3_evstpf_up= (l3_evstpf >0)* l3_evstpf;

L4_evpro_up= (l4_evpro >0)* l4_evpro;
L4_evcom_up= (l4_evcom >0)* l4_evcom;
L4_evprmp_up= (l4_evprmp >0) * l4_evprmp;
L4_ISALMTOT2_up= (l4_isalmtot2 >0)* l4_isalmtot2;
L4_TXIPP2_up = (l4_txipp2 >0) * l4_txipp2;
L4_evstpf_up= (l4_evstpf >0)* l4_evstpf;

L5_evpro_up= (l5_evpro >0)* l5_evpro;
L5_evcom_up= (l5_evcom >0)* l5_evcom;
L5_evprmp_up= (l5_evprmp >0) * l5_evprmp;
L5_ISALMTOT2_up= (l5_isalmtot2 >0)* l5_isalmtot2;
L5_TXIPP2_up = (l5_txipp2 >0) * l5_txipp2;
L5_evstpf_up= (l5_evstpf >0)* l5_evstpf;

L6_evpro_up= (l6_evpro >0)* l6_evpro;
L6_evcom_up= (l6_evcom >0)* l6_evcom;
L6_evprmp_up= (l6_evprmp >0) * l6_evprmp;
L6_ISALMTOT2_up= (l6_isalmtot2 >0)* l6_isalmtot2;
L6_TXIPP2_up = (l6_txipp2 >0) * l6_txipp2;
L6_evstpf_up= (l6_evstpf >0)* l6_evstpf;
      
L1_evpro_down= (l1_evpro <0)* l1_evpro;
L1_evcom_down= (l1_evcom <0)* l1_evcom;
L1_evprmp_down= (l1_evprmp <0) * l1_evprmp;
L1_ISALMTOT2_down= (l1_isalmtot2 <0)* l1_isalmtot2;
L1_TXIPP2_down = (l1_txipp2 <0) * l1_txipp2;
L1_evstpf_down= (l1_evstpf <0)* l1_evstpf;

L2_evpro_down= (l2_evpro <0)* l2_evpro;
L2_evcom_down= (l2_evcom <0)* l2_evcom;
L2_evprmp_down= (l2_evprmp <0) * l2_evprmp;
L2_ISALMTOT2_down= (l2_isalmtot2 <0)* l2_isalmtot2;
L2_TXIPP2_down = (l2_txipp2 <0) * l2_txipp2;
L2_evstpf_down= (l2_evstpf <0)* l2_evstpf;

L3_evpro_down= (l3_evpro <0)* l3_evpro;
L3_evcom_down= (l3_evcom <0)* l3_evcom;
L3_evprmp_down= (l3_evprmp <0) * l3_evprmp;
L3_ISALMTOT2_down= (l3_isalmtot2 <0)* l3_isalmtot2;
L3_TXIPP2_down = (l3_txipp2 <0) * l3_txipp2;
L3_evstpf_down= (l3_evstpf <0)* l3_evstpf;

L4_evpro_down= (l4_evpro <0)* l4_evpro;
L4_evcom_down= (l4_evcom <0)* l4_evcom;
L4_evprmp_down= (l4_evprmp <0) * l4_evprmp;
L4_ISALMTOT2_down= (l4_isalmtot2 <0)* l4_isalmtot2;
L4_TXIPP2_down = (l4_txipp2 <0) * l4_txipp2;
L4_evstpf_down= (l4_evstpf <0)* l4_evstpf;

L5_evpro_down= (l5_evpro <0)* l5_evpro;
L5_evcom_down= (l5_evcom <0)* l5_evcom;
L5_evprmp_down= (l5_evprmp <0) * l5_evprmp;
L5_ISALMTOT2_down= (l5_isalmtot2 <0)* l5_isalmtot2;
L5_TXIPP2_down = (l5_txipp2 <0) * l5_txipp2;
L5_evstpf_down= (l5_evstpf <0)* l5_evstpf;

L6_evpro_down= (l6_evpro <0)* l6_evpro;
L6_evcom_down= (l6_evcom <0)* l6_evcom;
L6_evprmp_down= (l6_evprmp <0) * l6_evprmp;
L6_ISALMTOT2_down= (l6_isalmtot2 <0)* l6_isalmtot2;
L6_TXIPP2_down = (l6_txipp2 <0) * l6_txipp2;
L6_evstpf_down= (l6_evstpf <0)* l6_evstpf;
run;


proc sort data = temp.stata_base_q3_asym;
by siren etabl ape  ;
run;


data temp.stata_base_q3_asym_chocs; set temp.stata_base_q3_asym;

evprpf_perm3 = ( ((evprpf ne 0 ) & (evprpf*l1_evprpf>0)) + ((evprpf ne 0 ) & (evprpf*l2_evprpf>0)) ) >0;

iip_perm3 = ( ((evprmp ne 0 ) & (evprmp*l1_evprmp>0)) + ((evprmp ne 0 ) & (evprmp*l2_evprmp>0)) ) >0;
l1_iip_perm3 = ( ((l1_evprmp ne 0 ) & (l1_evprmp*l2_evprmp>0)) + ((l1_evprmp ne 0 ) & (l1_evprmp*l3_evprmp>0)) ) >0;
l2_iip_perm3 = ( ((l2_evprmp ne 0 ) & (l2_evprmp*l3_evprmp>0)) + ((l2_evprmp ne 0 ) & (l2_evprmp*l4_evprmp>0))) >0;
l3_iip_perm3 = ( ((l3_evprmp ne 0 ) & (l3_evprmp*l4_evprmp>0)) + ((l3_evprmp ne 0 ) & (l3_evprmp*l5_evprmp>0))) >0;
l4_iip_perm3 = ( ((l4_evprmp ne 0 ) & (l4_evprmp*l5_evprmp>0)) + ((l4_evprmp ne 0 ) & (l4_evprmp*l6_evprmp>0))) >0;
l5_iip_perm3 = ( ((l5_evprmp ne 0 ) & (l5_evprmp*l6_evprmp>0)) + ((l5_evprmp ne 0 ) & (l5_evprmp*l7_evprmp>0))) >0;
l6_iip_perm3 = ( ((l6_evprmp ne 0 ) & (l6_evprmp*l7_evprmp>0)) + ((l6_evprmp ne 0 ) & (l6_evprmp*l8_evprmp>0))) >0;

prod_perm3 = ( ((evpro ne 0 ) & (evpro*l1_evpro>0)) + ((evpro ne 0 ) & (evpro*l2_evpro>0)) ) >0;
l1_prod_perm3 = ( ((l1_evpro ne 0 ) & (l1_evpro*l2_evpro>0)) + ((l1_evpro ne 0 ) & (l1_evpro*l3_evpro>0)) ) >0;
l2_prod_perm3 = ( ((l2_evpro ne 0 ) & (l2_evpro*l3_evpro>0)) + ((l2_evpro ne 0 ) & (l2_evpro*l4_evpro>0))) >0;
l3_prod_perm3 = ( ((l3_evpro ne 0 ) & (l3_evpro*l4_evpro>0)) + ((l3_evpro ne 0 ) & (l3_evpro*l5_evpro>0))) >0;
l4_prod_perm3 = ( ((l4_evpro ne 0 ) & (l4_evpro*l5_evpro>0)) + ((l4_evpro ne 0 ) & (l4_evpro*l6_evpro>0))) >0;
l5_prod_perm3 = ( ((l5_evpro ne 0 ) & (l5_evpro*l6_evpro>0)) + ((l5_evpro ne 0 ) & (l5_evpro*l7_evpro>0))) >0;
l6_prod_perm3 = ( ((l6_evpro ne 0 ) & (l6_evpro*l7_evpro>0)) + ((l6_evpro ne 0 ) & (l6_evpro*l8_evpro>0))) >0;

ord_perm3 = ( ((evcom ne 0 ) & (evcom*l1_evcom>0)) + ((evcom ne 0 ) & (evcom*l2_evcom>0)) ) >0;
l1_ord_perm3 = ( ((l1_evcom ne 0 ) & (l1_evcom*l2_evcom>0)) + ((l1_evcom ne 0 ) & (l1_evcom*l3_evcom>0)) ) >0;
l2_ord_perm3 = ( ((l2_evcom ne 0 ) & (l2_evcom*l3_evcom>0)) + ((l2_evcom ne 0 ) & (l2_evcom*l4_evcom>0))) >0;
l3_ord_perm3 = ( ((l3_evcom ne 0 ) & (l3_evcom*l4_evcom>0)) + ((l3_evcom ne 0 ) & (l3_evcom*l5_evcom>0))) >0;
l4_ord_perm3 = ( ((l4_evcom ne 0 ) & (l4_evcom*l5_evcom>0)) + ((l4_evcom ne 0 ) & (l4_evcom*l6_evcom>0))) >0;
l5_ord_perm3 = ( ((l5_evcom ne 0 ) & (l5_evcom*l6_evcom>0)) + ((l5_evcom ne 0 ) & (l5_evcom*l7_evcom>0))) >0;
l6_ord_perm3 = ( ((l6_evcom ne 0 ) & (l6_evcom*l7_evcom>0)) + ((l6_evcom ne 0 ) & (l6_evcom*l8_evcom>0))) >0;

inv_perm3 = ( ((evstpf ne 0 ) & (evstpf*l1_evstpf>0)) + ((evstpf ne 0 ) & (evstpf*l2_evstpf>0)) ) >0;
l1_inv_perm3 = ( ((l1_evstpf ne 0 ) & (l1_evstpf*l2_evstpf>0)) + ((l1_evstpf ne 0 ) & (l1_evstpf*l3_evstpf>0)) ) >0;
l2_inv_perm3 = ( ((l2_evstpf ne 0 ) & (l2_evstpf*l3_evstpf>0)) + ((l2_evstpf ne 0 ) & (l2_evstpf*l4_evstpf>0))) >0;
l3_inv_perm3 = ( ((l3_evstpf ne 0 ) & (l3_evstpf*l4_evstpf>0)) + ((l3_evstpf ne 0 ) & (l3_evstpf*l5_evstpf>0))) >0;
l4_inv_perm3 = ( ((l4_evstpf ne 0 ) & (l4_evstpf*l5_evstpf>0)) + ((l4_evstpf ne 0 ) & (l4_evstpf*l6_evstpf>0))) >0;
l5_inv_perm3 = ( ((l5_evstpf ne 0 ) & (l5_evstpf*l6_evstpf>0)) + ((l5_evstpf ne 0 ) & (l5_evstpf*l7_evstpf>0))) >0;
l6_inv_perm3 = ( ((l6_evstpf ne 0 ) & (l6_evstpf*l7_evstpf>0)) + ((l6_evstpf ne 0 ) & (l6_evstpf*l8_evstpf>0))) >0;

ipp_perm3 = ( ((txipp2 ne 0 ) & (txipp2*l1_txipp2>0)) + ((txipp2 ne 0 ) & (txipp2*l2_txipp2>0)) ) >0;
l1_ipp_perm3 = ( ((l1_txipp2 ne 0 ) & (l1_txipp2*l2_txipp2>0)) + ((l1_txipp2 ne 0 ) & (l1_txipp2*l3_txipp2>0)) ) >0;
l2_ipp_perm3 = ( ((l2_txipp2 ne 0 ) & (l2_txipp2*l3_txipp2>0)) + ((l2_txipp2 ne 0 ) & (l2_txipp2*l4_txipp2>0))) >0;
l3_ipp_perm3 = ( ((l3_txipp2 ne 0 ) & (l3_txipp2*l4_txipp2>0)) + ((l3_txipp2 ne 0 ) & (l3_txipp2*l5_txipp2>0))) >0;
l4_ipp_perm3 = ( ((l4_txipp2 ne 0 ) & (l4_txipp2*l5_txipp2>0)) + ((l4_txipp2 ne 0 ) & (l4_txipp2*l6_txipp2>0))) >0;
l5_ipp_perm3 = ( ((l5_txipp2 ne 0 ) & (l5_txipp2*l6_txipp2>0)) + ((l5_txipp2 ne 0 ) & (l5_txipp2*l7_txipp2>0))) >0;
l6_ipp_perm3 = ( ((l6_txipp2 ne 0 ) & (l6_txipp2*l7_txipp2>0)) + ((l6_txipp2 ne 0 ) & (l6_txipp2*l7_txipp2>0))) >0;
run;

data temp.stata_base_q3_asym_chocs; set temp.stata_base_q3_asym_chocs;

evprpf_i3_up = evprpf_up * (1-evprpf_perm3);
evprpf_p3_up = evprpf_up * evprpf_perm3;

evprmp_f1_i3_up = evprmp_f1_up * (1-iip_perm3);
evprmp_i3_up = evprmp_up * (1-iip_perm3);
evprmp_l1_i3_up = evprmp_l1_up * (1-l1_iip_perm3);
evprmp_l2_i3_up = evprmp_l2_up * (1-l2_iip_perm3);
evprmp_l3_i3_up = evprmp_l3_up * (1-l3_iip_perm3);
 
evprmp_f1_p3_up = evprmp_f1_up * iip_perm3;
evprmp_p3_up = evprmp_up * iip_perm3;
evprmp_l1_p3_up = evprmp_l1_up * l1_iip_perm3;
evprmp_l2_p3_up = evprmp_l2_up * l2_iip_perm3;
evprmp_l3_p3_up = evprmp_l3_up * l3_iip_perm3;

prevpro_i3_up = prevpro_up * (1-prod_perm3);
evpro_i3_up = evpro_up * (1-prod_perm3);
evpro_l1_i3_up = evpro_l1_up * (1-l1_prod_perm3);
evpro_l2_i3_up = evpro_l2_up * (1-l2_prod_perm3);
evpro_l3_i3_up = evpro_l3_up * (1-l3_prod_perm3);
 
prevpro_p3_up = prevpro_up * prod_perm3;
evpro_p3_up = evpro_up * prod_perm3;
evpro_l1_p3_up = evpro_l1_up * l1_prod_perm3;
evpro_l2_p3_up = evpro_l2_up * l2_prod_perm3;
evpro_l3_p3_up = evpro_l3_up * l3_prod_perm3;

evcom_f1_i3_up = evcom_f1_up * (1-ord_perm3);
evcom_i3_up = evcom_up * (1-ord_perm3);
evcom_l1_i3_up = evcom_l1_up * (1-l1_ord_perm3);
evcom_l2_i3_up = evcom_l2_up * (1-l2_ord_perm3);
evcom_l3_i3_up = evcom_l3_up * (1-l3_ord_perm3);
 
evcom_f1_p3_up = evcom_f1_up * ord_perm3;
evcom_p3_up = evcom_up * ord_perm3;
evcom_l1_p3_up = evcom_l1_up * l1_ord_perm3;
evcom_l2_p3_up = evcom_l2_up * l2_ord_perm3;
evcom_l3_p3_up = evcom_l3_up * l3_ord_perm3;

evstpf_f1_i3_up = evstpf_f1_up * (1-inv_perm3);
evstpf_i3_up = evstpf_up * (1-inv_perm3);
evstpf_l1_i3_up = evstpf_l1_up * (1-l1_inv_perm3);
evstpf_l2_i3_up = evstpf_l2_up * (1-l2_inv_perm3);
evstpf_l3_i3_up = evstpf_l3_up * (1-l3_inv_perm3);
 
evstpf_f1_p3_up = evstpf_f1_up * inv_perm3;
evstpf_p3_up = evstpf_up * inv_perm3;
evstpf_l1_p3_up = evstpf_l1_up * l1_inv_perm3;
evstpf_l2_p3_up = evstpf_l2_up * l2_inv_perm3;
evstpf_l3_p3_up = evstpf_l3_up * l3_inv_perm3;

txipp2_f1_i3_up = txipp2_f1_up * (1-ipp_perm3);
txipp2_i3_up = txipp2_up * (1-ipp_perm3);
txipp2_l1_i3_up = txipp2_l1_up * (1-l1_ipp_perm3);
txipp2_l2_i3_up = txipp2_l2_up * (1-l2_ipp_perm3);
txipp2_l3_i3_up = txipp2_l3_up * (1-l3_ipp_perm3);
 
txipp2_f1_p3_up = txipp2_f1_up * ipp_perm3;
txipp2_p3_up = txipp2_up * ipp_perm3;
txipp2_l1_p3_up = txipp2_l1_up * l1_ipp_perm3;
txipp2_l2_p3_up = txipp2_l2_up * l2_ipp_perm3;
txipp2_l3_p3_up = txipp2_l3_up * l3_ipp_perm3;


l1_evprmp_i3_up = l1_evprmp_up * (1-l1_iip_perm3);
l2_evprmp_i3_up = l2_evprmp_up * (1-l2_iip_perm3);
l3_evprmp_i3_up = l3_evprmp_up * (1-l3_iip_perm3);
l4_evprmp_i3_up = l4_evprmp_up * (1-l4_iip_perm3);
l5_evprmp_i3_up = l5_evprmp_up * (1-l5_iip_perm3);
l6_evprmp_i3_up = l6_evprmp_up * (1-l6_iip_perm3);

l1_evprmp_p3_up = l1_evprmp_up * l1_iip_perm3;
l2_evprmp_p3_up = l2_evprmp_up * l2_iip_perm3;
l3_evprmp_p3_up = l3_evprmp_up * l3_iip_perm3;
l4_evprmp_p3_up = l4_evprmp_up * l4_iip_perm3;
l5_evprmp_p3_up = l5_evprmp_up * l5_iip_perm3;
l6_evprmp_p3_up = l6_evprmp_up * l6_iip_perm3;

l1_evpro_i3_up = l1_evpro_up * (1-l1_prod_perm3);
l2_evpro_i3_up = l2_evpro_up * (1-l2_prod_perm3);
l3_evpro_i3_up = l3_evpro_up * (1-l3_prod_perm3);
l4_evpro_i3_up = l4_evpro_up * (1-l4_prod_perm3);
l5_evpro_i3_up = l5_evpro_up * (1-l5_prod_perm3);
l6_evpro_i3_up = l6_evpro_up * (1-l6_prod_perm3);

l1_evpro_p3_up = l1_evpro_up * l1_prod_perm3;
l2_evpro_p3_up = l2_evpro_up * l2_prod_perm3;
l3_evpro_p3_up = l3_evpro_up * l3_prod_perm3;
l4_evpro_p3_up = l4_evpro_up * l4_prod_perm3;
l5_evpro_p3_up = l5_evpro_up * l5_prod_perm3;
l6_evpro_p3_up = l6_evpro_up * l6_prod_perm3;

l1_evcom_i3_up = l1_evcom_up * (1-l1_ord_perm3);
l2_evcom_i3_up = l2_evcom_up * (1-l2_ord_perm3);
l3_evcom_i3_up = l3_evcom_up * (1-l3_ord_perm3);
l4_evcom_i3_up = l4_evcom_up * (1-l4_ord_perm3);
l5_evcom_i3_up = l5_evcom_up * (1-l5_ord_perm3);
l6_evcom_i3_up = l6_evcom_up * (1-l6_ord_perm3);

l1_evcom_p3_up = l1_evcom_up * l1_ord_perm3;
l2_evcom_p3_up = l2_evcom_up * l2_ord_perm3;
l3_evcom_p3_up = l3_evcom_up * l3_ord_perm3;
l4_evcom_p3_up = l4_evcom_up * l4_ord_perm3;
l5_evcom_p3_up = l5_evcom_up * l5_ord_perm3;
l6_evcom_p3_up = l6_evcom_up * l6_ord_perm3;

l1_evstpf_i3_up = l1_evstpf_up * (1-l1_inv_perm3);
l2_evstpf_i3_up = l2_evstpf_up * (1-l2_inv_perm3);
l3_evstpf_i3_up = l3_evstpf_up * (1-l3_inv_perm3);
l4_evstpf_i3_up = l4_evstpf_up * (1-l4_inv_perm3);
l5_evstpf_i3_up = l5_evstpf_up * (1-l5_inv_perm3);
l6_evstpf_i3_up = l6_evstpf_up * (1-l6_inv_perm3);

l1_evstpf_p3_up = l1_evstpf_up * l1_inv_perm3;
l2_evstpf_p3_up = l2_evstpf_up * l2_inv_perm3;
l3_evstpf_p3_up = l3_evstpf_up * l3_inv_perm3;
l4_evstpf_p3_up = l4_evstpf_up * l4_inv_perm3;
l5_evstpf_p3_up = l5_evstpf_up * l5_inv_perm3;
l6_evstpf_p3_up = l6_evstpf_up * l6_inv_perm3;

l1_txipp2_i3_up = l1_txipp2_up * (1-l1_ipp_perm3);
l2_txipp2_i3_up = l2_txipp2_up * (1-l2_ipp_perm3);
l3_txipp2_i3_up = l3_txipp2_up * (1-l3_ipp_perm3);
l4_txipp2_i3_up = l4_txipp2_up * (1-l4_ipp_perm3);
l5_txipp2_i3_up = l5_txipp2_up * (1-l5_ipp_perm3);
l6_txipp2_i3_up = l6_txipp2_up * (1-l6_ipp_perm3);

l1_txipp2_p3_up = l1_txipp2_up * l1_ipp_perm3;
l2_txipp2_p3_up = l2_txipp2_up * l2_ipp_perm3;
l3_txipp2_p3_up = l3_txipp2_up * l3_ipp_perm3;
l4_txipp2_p3_up = l4_txipp2_up * l4_ipp_perm3;
l5_txipp2_p3_up = l5_txipp2_up * l5_ipp_perm3;
l6_txipp2_p3_up = l6_txipp2_up * l6_ipp_perm3;

run;

data temp.stata_base_q3_asym_chocs; set temp.stata_base_q3_asym_chocs;
evprpf_i3_down = evprpf_down * (1-evprpf_perm3);
evprpf_p3_down = evprpf_down * evprpf_perm3;

evprmp_f1_i3_down = evprmp_f1_down * (1-iip_perm3);
evprmp_i3_down = evprmp_down * (1-iip_perm3);
evprmp_l1_i3_down = evprmp_l1_down * (1-l1_iip_perm3);
evprmp_l2_i3_down = evprmp_l2_down * (1-l2_iip_perm3);
evprmp_l3_i3_down = evprmp_l3_down * (1-l3_iip_perm3);
 
evprmp_f1_p3_down = evprmp_f1_down * iip_perm3;
evprmp_p3_down = evprmp_down * iip_perm3;
evprmp_l1_p3_down = evprmp_l1_down * l1_iip_perm3;
evprmp_l2_p3_down = evprmp_l2_down * l2_iip_perm3;
evprmp_l3_p3_down = evprmp_l3_down * l3_iip_perm3;

prevpro_i3_down = prevpro_down * (1-prod_perm3);
evpro_i3_down = evpro_down * (1-prod_perm3);
evpro_l1_i3_down = evpro_l1_down * (1-l1_prod_perm3);
evpro_l2_i3_down = evpro_l2_down * (1-l2_prod_perm3);
evpro_l3_i3_down = evpro_l3_down * (1-l3_prod_perm3);
 
prevpro_p3_down = prevpro_down * prod_perm3;
evpro_p3_down = evpro_down * prod_perm3;
evpro_l1_p3_down = evpro_l1_down * l1_prod_perm3;
evpro_l2_p3_down = evpro_l2_down * l2_prod_perm3;
evpro_l3_p3_down = evpro_l3_down * l3_prod_perm3;

evcom_f1_i3_down = evcom_f1_down * (1-ord_perm3);
evcom_i3_down = evcom_down * (1-ord_perm3);
evcom_l1_i3_down = evcom_l1_down * (1-l1_ord_perm3);
evcom_l2_i3_down = evcom_l2_down * (1-l2_ord_perm3);
evcom_l3_i3_down = evcom_l3_down * (1-l3_ord_perm3);
 
evcom_f1_p3_down = evcom_f1_down * ord_perm3;
evcom_p3_down = evcom_down * ord_perm3;
evcom_l1_p3_down = evcom_l1_down * l1_ord_perm3;
evcom_l2_p3_down = evcom_l2_down * l2_ord_perm3;
evcom_l3_p3_down = evcom_l3_down * l3_ord_perm3;

evstpf_f1_i3_down = evstpf_f1_down * (1-inv_perm3);
evstpf_i3_down = evstpf_down * (1-inv_perm3);
evstpf_l1_i3_down = evstpf_l1_down * (1-l1_inv_perm3);
evstpf_l2_i3_down = evstpf_l2_down * (1-l2_inv_perm3);
evstpf_l3_i3_down = evstpf_l3_down * (1-l3_inv_perm3);
 
evstpf_f1_p3_down = evstpf_f1_down * inv_perm3;
evstpf_p3_down = evstpf_down * inv_perm3;
evstpf_l1_p3_down = evstpf_l1_down * l1_inv_perm3;
evstpf_l2_p3_down = evstpf_l2_down * l2_inv_perm3;
evstpf_l3_p3_down = evstpf_l3_down * l3_inv_perm3;

txipp2_f1_i3_down = txipp2_f1_down * (1-ipp_perm3);
txipp2_i3_down = txipp2_down * (1-ipp_perm3);
txipp2_l1_i3_down = txipp2_l1_down * (1-l1_ipp_perm3);
txipp2_l2_i3_down = txipp2_l2_down * (1-l2_ipp_perm3);
txipp2_l3_i3_down = txipp2_l3_down * (1-l3_ipp_perm3);
 
txipp2_f1_p3_down = txipp2_f1_down * ipp_perm3;
txipp2_p3_down = txipp2_down * ipp_perm3;
txipp2_l1_p3_down = txipp2_l1_down * l1_ipp_perm3;
txipp2_l2_p3_down = txipp2_l2_down * l2_ipp_perm3;
txipp2_l3_p3_down = txipp2_l3_down * l3_ipp_perm3;


l1_evprmp_i3_down = l1_evprmp_down * (1-l1_iip_perm3);
l2_evprmp_i3_down = l2_evprmp_down * (1-l2_iip_perm3);
l3_evprmp_i3_down = l3_evprmp_down * (1-l3_iip_perm3);
l4_evprmp_i3_down = l4_evprmp_down * (1-l4_iip_perm3);
l5_evprmp_i3_down = l5_evprmp_down * (1-l5_iip_perm3);
l6_evprmp_i3_down = l6_evprmp_down * (1-l6_iip_perm3);

l1_evprmp_p3_down = l1_evprmp_down * l1_iip_perm3;
l2_evprmp_p3_down = l2_evprmp_down * l2_iip_perm3;
l3_evprmp_p3_down = l3_evprmp_down * l3_iip_perm3;
l4_evprmp_p3_down = l4_evprmp_down * l4_iip_perm3;
l5_evprmp_p3_down = l5_evprmp_down * l5_iip_perm3;
l6_evprmp_p3_down = l6_evprmp_down * l6_iip_perm3;

l1_evpro_i3_down = l1_evpro_down * (1-l1_prod_perm3);
l2_evpro_i3_down = l2_evpro_down * (1-l2_prod_perm3);
l3_evpro_i3_down = l3_evpro_down * (1-l3_prod_perm3);
l4_evpro_i3_down = l4_evpro_down * (1-l4_prod_perm3);
l5_evpro_i3_down = l5_evpro_down * (1-l5_prod_perm3);
l6_evpro_i3_down = l6_evpro_down * (1-l6_prod_perm3);

l1_evpro_p3_down = l1_evpro_down * l1_prod_perm3;
l2_evpro_p3_down = l2_evpro_down * l2_prod_perm3;
l3_evpro_p3_down = l3_evpro_down * l3_prod_perm3;
l4_evpro_p3_down = l4_evpro_down * l4_prod_perm3;
l5_evpro_p3_down = l5_evpro_down * l5_prod_perm3;
l6_evpro_p3_down = l6_evpro_down * l6_prod_perm3;

l1_evcom_i3_down = l1_evcom_down * (1-l1_ord_perm3);
l2_evcom_i3_down = l2_evcom_down * (1-l2_ord_perm3);
l3_evcom_i3_down = l3_evcom_down * (1-l3_ord_perm3);
l4_evcom_i3_down = l4_evcom_down * (1-l4_ord_perm3);
l5_evcom_i3_down = l5_evcom_down * (1-l5_ord_perm3);
l6_evcom_i3_down = l6_evcom_down * (1-l6_ord_perm3);

l1_evcom_p3_down = l1_evcom_down * l1_ord_perm3;
l2_evcom_p3_down = l2_evcom_down * l2_ord_perm3;
l3_evcom_p3_down = l3_evcom_down * l3_ord_perm3;
l4_evcom_p3_down = l4_evcom_down * l4_ord_perm3;
l5_evcom_p3_down = l5_evcom_down * l5_ord_perm3;
l6_evcom_p3_down = l6_evcom_down * l6_ord_perm3;

l1_evstpf_i3_down = l1_evstpf_down * (1-l1_inv_perm3);
l2_evstpf_i3_down = l2_evstpf_down * (1-l2_inv_perm3);
l3_evstpf_i3_down = l3_evstpf_down * (1-l3_inv_perm3);
l4_evstpf_i3_down = l4_evstpf_down * (1-l4_inv_perm3);
l5_evstpf_i3_down = l5_evstpf_down * (1-l5_inv_perm3);
l6_evstpf_i3_down = l6_evstpf_down * (1-l6_inv_perm3);

l1_evstpf_p3_down = l1_evstpf_down * l1_inv_perm3;
l2_evstpf_p3_down = l2_evstpf_down * l2_inv_perm3;
l3_evstpf_p3_down = l3_evstpf_down * l3_inv_perm3;
l4_evstpf_p3_down = l4_evstpf_down * l4_inv_perm3;
l5_evstpf_p3_down = l5_evstpf_down * l5_inv_perm3;
l6_evstpf_p3_down = l6_evstpf_down * l6_inv_perm3;

l1_txipp2_i3_down = l1_txipp2_down * (1-l1_ipp_perm3);
l2_txipp2_i3_down = l2_txipp2_down * (1-l2_ipp_perm3);
l3_txipp2_i3_down = l3_txipp2_down * (1-l3_ipp_perm3);
l4_txipp2_i3_down = l4_txipp2_down * (1-l4_ipp_perm3);
l5_txipp2_i3_down = l5_txipp2_down * (1-l5_ipp_perm3);
l6_txipp2_i3_down = l6_txipp2_down * (1-l6_ipp_perm3);

l1_txipp2_p3_down = l1_txipp2_down * l1_ipp_perm3;
l2_txipp2_p3_down = l2_txipp2_down * l2_ipp_perm3;
l3_txipp2_p3_down = l3_txipp2_down * l3_ipp_perm3;
l4_txipp2_p3_down = l4_txipp2_down * l4_ipp_perm3;
l5_txipp2_p3_down = l5_txipp2_down * l5_ipp_perm3;
l6_txipp2_p3_down = l6_txipp2_down * l6_ipp_perm3;

run;

DATA TEMP.STATA_BASE_Q3_ASYM_CHOCS; SET TEMP.STATA_BASE_Q3_ASYM_CHOCS; 
keep ident_stata siren etabl ape annee mois numenq naf_gc 
DUR_1-DUR_14 DUR15_24 DUR25_00 MOIS_1-MOIS_12 ANNEE_1998-ANNEE_2005 TRIM1-TRIM4
tva_2000 tva_2000_2 euro_2002 euro_2002_2
ind_DA ind_DB ind_DC ind_DD ind_DE ind_DG ind_DH ind_DI ind_DJ ind_DK ind_DL ind_DM ind_DN 
evprpf   evprmp   evpro   evcom   evstpf   txipp2   isalmtot2  
l1_evprmp l2_evprmp l3_evprmp l4_evprmp l5_evprmp   l6_evprmp   
l1_evpro   l2_evpro l3_evpro l4_evpro l5_evpro   l6_evpro    
l1_evcom   l2_evcom l3_evcom l4_evcom l5_evcom   l6_evcom    
l1_evstpf   l2_evstpf l3_evstpf l4_evstpf l5_evstpf   l6_evstpf   
l1_txipp2   l2_txipp2 l3_txipp2 l4_txipp2 l5_txipp2   l6_txipp2   
l1_isalmtot2   l2_isalmtot2 l3_isalmtot2 l4_isalmtot2 l5_isalmtot2 l6_isalmtot2  
y_null prevpro evprmp_f1 evcom_f1 prevstpf isalmtot2_f1 txipp2_f1 
CUM_ISALMTOT CUM_ISALMTOT_L123 ISALMTOT2_L1 ISALMTOT2_L2 ISALMTOT2_L3
CUM_EVPRMP CUM_EVPRMP_L123 EVPRMP_L1 EVPRMP_L2 EVPRMP_L3
CUM_EVCOM CUM_EVCOM_L123 EVCOM_L1 EVCOM_L2 EVCOM_L3 
CUM_IPP2 CUM_IPP2_L123 TXIPP2_L1 TXIPP2_L2 TXIPP2_L3 
CUM_EVPRO CUM_EVPRO_L123 EVPRO_L1 EVPRO_L2 EVPRO_L3 
CUM_EVSTPF CUM_EVSTPF_L123 EVSTPF_L1 EVSTPF_L2 EVSTPF_L3
isalmtot2_up isalmtot2_l1_up isalmtot2_l2_up isalmtot2_l3_up 
isalmtot2_down isalmtot2_l1_down isalmtot2_l2_down isalmtot2_l3_down 
cum_evprmp_l123_up cum_isalmtot_l123_up cum_evpro_l123_up cum_ipp2_l123_up  
cum_evprmp_l123_down cum_isalmtot_l123_down cum_evpro_l123_down cum_ipp2_l123_down  
evprpf_perm3 iip_perm3 prod_perm3 ord_perm3 inv_perm3 ipp_perm3 
cum_evprmp_l123_down  cum_isalmtot_l123_down cum_evpro_l123_down  cum_ipp2_l123_down 
evprpf_i3_up evprpf_p3_up evprpf_i3_down evprpf_p3_down 
evprmp_f1_i3_up evprmp_i3_up evprmp_l1_i3_up evprmp_l2_i3_up evprmp_l3_i3_up 
evprmp_f1_p3_up evprmp_p3_up evprmp_l1_p3_up evprmp_l2_p3_up evprmp_l3_p3_up
prevpro_i3_up   evpro_i3_up  evpro_l1_i3_up  evpro_l2_i3_up  evpro_l3_i3_up
prevpro_p3_up   evpro_p3_up  evpro_l1_p3_up  evpro_l2_p3_up  evpro_l3_p3_up 
evcom_f1_i3_up  evcom_i3_up  evcom_l1_i3_up  evcom_l2_i3_up  evcom_l3_i3_up 
evcom_f1_p3_up  evcom_p3_up  evcom_l1_p3_up  evcom_l2_p3_up  evcom_l3_p3_up
evstpf_f1_i3_up evstpf_i3_up evstpf_l1_i3_up evstpf_l2_i3_up evstpf_l3_i3_up  
evstpf_f1_p3_up evstpf_p3_up evstpf_l1_p3_up evstpf_l2_p3_up evstpf_l3_p3_up
txipp2_f1_i3_up txipp2_i3_up txipp2_l1_i3_up txipp2_l2_i3_up txipp2_l3_i3_up 
txipp2_f1_p3_up txipp2_p3_up txipp2_l1_p3_up txipp2_l2_p3_up txipp2_l3_p3_up 
l1_evprmp_i3_up l2_evprmp_i3_up l3_evprmp_i3_up l4_evprmp_i3_up l5_evprmp_i3_up l6_evprmp_i3_up
l1_evprmp_p3_up l2_evprmp_p3_up l3_evprmp_p3_up l4_evprmp_p3_up l5_evprmp_p3_up l6_evprmp_p3_up 
l1_evpro_i3_up  l2_evpro_i3_up  l3_evpro_i3_up  l4_evpro_i3_up  l5_evpro_i3_up  l6_evpro_i3_up 
l1_evpro_p3_up  l2_evpro_p3_up  l3_evpro_p3_up  l4_evpro_p3_up  l5_evpro_p3_up  l6_evpro_p3_up
l1_evcom_i3_up  l2_evcom_i3_up  l3_evcom_i3_up  l4_evcom_i3_up  l5_evcom_i3_up  l6_evcom_i3_up 
l1_evcom_p3_up  l2_evcom_p3_up  l3_evcom_p3_up  l4_evcom_p3_up  l5_evcom_p3_up  l6_evcom_p3_up 
l1_evstpf_i3_up l2_evstpf_i3_up l3_evstpf_i3_up l4_evstpf_i3_up l5_evstpf_i3_up l6_evstpf_i3_up 
l1_evstpf_p3_up l2_evstpf_p3_up l3_evstpf_p3_up l4_evstpf_p3_up l5_evstpf_p3_up l6_evstpf_p3_up
l1_txipp2_i3_up l2_txipp2_i3_up l3_txipp2_i3_up l4_txipp2_i3_up l5_txipp2_i3_up l6_txipp2_i3_up 
l1_txipp2_p3_up l2_txipp2_p3_up l3_txipp2_p3_up l4_txipp2_p3_up l5_txipp2_p3_up l6_txipp2_p3_up
evprmp_f1_i3_down evprmp_i3_down evprmp_l1_i3_down evprmp_l2_i3_down evprmp_l3_i3_down 
evprmp_f1_p3_down evprmp_p3_down evprmp_l1_p3_down evprmp_l2_p3_down evprmp_l3_p3_down
prevpro_i3_down   evpro_i3_down  evpro_l1_i3_down  evpro_l2_i3_down  evpro_l3_i3_down
prevpro_p3_down   evpro_p3_down  evpro_l1_p3_down  evpro_l2_p3_down  evpro_l3_p3_down 
evcom_f1_i3_down  evcom_i3_down  evcom_l1_i3_down  evcom_l2_i3_down  evcom_l3_i3_down 
evcom_f1_p3_down  evcom_p3_down  evcom_l1_p3_down  evcom_l2_p3_down  evcom_l3_p3_down
evstpf_f1_i3_down evstpf_i3_down evstpf_l1_i3_down evstpf_l2_i3_down evstpf_l3_i3_down  
evstpf_f1_p3_down evstpf_p3_down evstpf_l1_p3_down evstpf_l2_p3_down evstpf_l3_p3_down
txipp2_f1_i3_down txipp2_i3_down txipp2_l1_i3_down txipp2_l2_i3_down txipp2_l3_i3_down 
txipp2_f1_p3_down txipp2_p3_down txipp2_l1_p3_down txipp2_l2_p3_down txipp2_l3_p3_down 
l1_evprmp_i3_down l2_evprmp_i3_down l3_evprmp_i3_down l4_evprmp_i3_down l5_evprmp_i3_down l6_evprmp_i3_down
l1_evprmp_p3_down l2_evprmp_p3_down l3_evprmp_p3_down l4_evprmp_p3_down l5_evprmp_p3_down l6_evprmp_p3_down 
l1_evpro_i3_down  l2_evpro_i3_down  l3_evpro_i3_down  l4_evpro_i3_down  l5_evpro_i3_down  l6_evpro_i3_down 
l1_evpro_p3_down  l2_evpro_p3_down  l3_evpro_p3_down  l4_evpro_p3_down  l5_evpro_p3_down  l6_evpro_p3_down
l1_evcom_i3_down  l2_evcom_i3_down  l3_evcom_i3_down  l4_evcom_i3_down  l5_evcom_i3_down  l6_evcom_i3_down 
l1_evcom_p3_down  l2_evcom_p3_down  l3_evcom_p3_down  l4_evcom_p3_down  l5_evcom_p3_down  l6_evcom_p3_down 
l1_evstpf_i3_down l2_evstpf_i3_down l3_evstpf_i3_down l4_evstpf_i3_down l5_evstpf_i3_down l6_evstpf_i3_down 
l1_evstpf_p3_down l2_evstpf_p3_down l3_evstpf_p3_down l4_evstpf_p3_down l5_evstpf_p3_down l6_evstpf_p3_down
l1_txipp2_i3_down l2_txipp2_i3_down l3_txipp2_i3_down l4_txipp2_i3_down l5_txipp2_i3_down l6_txipp2_i3_down 
l1_txipp2_p3_down l2_txipp2_p3_down l3_txipp2_p3_down l4_txipp2_p3_down l5_txipp2_p3_down l6_txipp2_p3_down
l1_isalmtot2_up l2_isalmtot2_up l3_isalmtot2_up l4_isalmtot2_up l5_isalmtot2_up l6_isalmtot2_up 
l1_isalmtot2_down l2_isalmtot2_down l3_isalmtot2_down l4_isalmtot2_down l5_isalmtot2_down l6_isalmtot2_down
;
run;

options notes;

PROC EXPORT DATA= TEMP.STATA_BASE_Q3_ASYM_CHOCS 
            OUTFILE= "C:\Users\SAMIC\Documents\Version_FINALE\Envoi_Final_REStat\Envoi_pgm\Verif_data\stata_base_q3_asym_chocs" 
            DBMS=STATA REPLACE;
RUN;

